<?php
/* *
 * 功能：即时到账批量退款有密接口接入页
 * 版本：3.4
 * 修改日期：2016-03-08
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。

 *************************注意*************************
 * 如果您在接口集成过程中遇到问题，可以按照下面的途径来解决
 * 1、开发文档中心（https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.oxen1k&treeId=66&articleId=103600&docType=1）
 * 2、商户帮助中心（https://cshall.alipay.com/enterprise/help_detail.htm?help_id=473888）
 * 3、支持中心（https://support.open.alipay.com/alipay/support/index.htm）
 * 如果不想使用扩展功能请把扩展功能参数赋空值。
 */
namespace Api\Controller;

use Shop\Api\OrderRefundMoneyApi;

class AlipayRefundController extends AlipayController{

    /**
     * 构造函数
     */
    public function __construct(){
        parent::__construct();
        $this->client_id=I('get.client_id','');
        $this->getConfig();
        //引入支付官方sdk
        vendor('alipay_submit',APP_PATH.'Api/Vendor/AlipayApp/lib','.class.php');
        vendor('alipay_notify',APP_PATH.'Api/Vendor/AlipayApp/lib','.class.php');

    }

    public function submit(){
        $alipay_config = $this->getAlipayRefundConfig();
        /**************************请求参数**************************/

        header('content-type:text/html;charset=utf8');
        if(IS_POST){
            //批次号，必填，格式：当天日期[8位]+序列号[3至24位]，如：201603081000001
            $batch_no = $_POST['WIDbatch_no'];
            //退款笔数，必填，参数detail_data的值中，“#”字符出现的数量加1，最大支持1000笔（即“#”字符出现的数量999个）
            $batch_num = $_POST['WIDbatch_num'];
            //退款详细数据，必填，格式（支付宝交易号^退款金额^备注），多笔请用#隔开
            $detail_data = $_POST['WIDdetail_data'];
        }else{
            //批次号，必填，格式：当天日期[8位]+序列号[3至24位]，如：201603081000001
            $batch_no = $_GET['WIDbatch_no'];
            //退款笔数，必填，参数detail_data的值中，“#”字符出现的数量加1，最大支持1000笔（即“#”字符出现的数量999个）
            $batch_num = 1;
            //退款详细数据，必填，格式（支付宝交易号^退款金额^备注），多笔请用#隔开
            $detail_data = urldecode($_GET['WIDdetail_data']);
        }

        /************************************************************/

        //构造要请求的参数数组，无需改动
        $parameter = array(
            "service" => trim($alipay_config['service']),
            "partner" => trim($alipay_config['partner']),
            "notify_url"	=> trim($alipay_config['notify_url']),
            "seller_user_id"	=> trim($alipay_config['seller_user_id']),
            "refund_date"	=> trim($alipay_config['refund_date']),
            "batch_no"	=> $batch_no,
            "batch_num"	=> $batch_num,
            "detail_data"	=> $detail_data,
            "_input_charset"	=> trim(strtolower($alipay_config['input_charset']))

        );

        //建立请求
        $alipaySubmit = new \AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");

        echo $html_text;
    }

    public function notify_url(){
        //计算得出通知验证结果
        $alipay_config = $this->getAlipayRefundConfig();
        $alipayNotify = new \AlipayNotify($alipay_config);
        $verify_result = $alipayNotify->verifyNotify();

        addLog('alipayRefund/notify_url',$_GET,$_POST,'阿里退款回调',true);
        if($verify_result) {//验证成功
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //请在这里加上商户的业务逻辑程序代


            //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——

            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表

            //批次号
            $batch_no = $_POST['batch_no'];

            //批量退款数据中转账成功的笔数
            $success_num = $_POST['success_num'];

            //批量退款数据中的详细信息
            $result_details = $_POST['result_details'];

            $this->refundSuccess($batch_no,$result_details);

            //判断是否在商户网站中已经做过了这次通知返回的处理
            //如果没有做过处理，那么执行商户的业务程序
            //如果有做过处理，那么不执行商户的业务程序

            echo "success";		//请不要修改或删除

            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");

            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
        else {
            //验证失败
            echo "fail";

            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
    }

    private function refundSuccess($batch_no,$result_details){

        apiCall(OrderRefundMoneyApi::REFUND_SUCCESS,array($batch_no,$result_details));
    }


    public function index(){

        echo
        '<!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>支付宝即时到账批量退款有密接口</title>
        </head>

        <style>
            html,body {
                width:100%;
                min-width:1200px;
                height:auto;
                padding:0;
                margin:0;
                font-family:"微软雅黑";
                background-color:#242736
            }
            .header {
                width:100%;
                margin:0 auto;
                height:230px;
                background-color:#fff
            }
            .container {
                width:100%;
                min-width:100px;
                height:auto
            }
            .black {
                background-color:#242736
            }
            .blue {
                background-color:#0ae
            }
            .qrcode {
                width:1200px;
                margin:0 auto;
                height:30px;
                background-color:#242736
            }
            .littlecode {
                width:16px;
                height:16px;
                margin-top:6px;
                cursor:pointer;
                float:right
            }
            .showqrs {
                top:30px;
                position:absolute;
                width:100px;
                margin-left:-65px;
                height:160px;
                display:none
            }
            .shtoparrow {
                width:0;
                height:0;
                margin-left:65px;
                border-left:8px solid transparent;
                border-right:8px solid transparent;
                border-bottom:8px solid #e7e8eb;
                margin-bottom:0;
                font-size:0;
                line-height:0
            }
            .guanzhuqr {
                text-align:center;
                background-color:#e7e8eb;
                border:1px solid #e7e8eb
            }
            .guanzhuqr img {
                margin-top:10px;
                width:80px
            }
            .shmsg {
                margin-left:10px;
                width:80px;
                height:16px;
                line-height:16px;
                font-size:12px;
                color:#242323;
                text-align:center
            }
            .nav {
                width:1200px;
                margin:0 auto;
                height:70px;
            }
            .open,.logo {
                display:block;
                float:left;
                height:40px;
                width:85px;
                margin-top:20px
            }
            .divier {
                display:block;
                float:left;
                margin-left:20px;
                margin-right:20px;
                margin-top:23px;
                width:1px;
                height:24px;
                background-color:#d3d3d3
            }
            .open {
                line-height:30px;
                font-size:20px;
                text-decoration:none;
                color:#1a1a1a
            }
            .navbar {
                float:right;
                width:200px;
                height:40px;
                margin-top:15px;
                list-style:none
            }
            .navbar li {
                float:left;
                width:100px;
                height:40px
            }
            .navbar li a {
                display:inline-block;
                width:100px;
                height:40px;
                line-height:40px;
                font-size:16px;
                color:#1a1a1a;
                text-decoration:none;
                text-align:center
            }
            .navbar li a:hover {
                color:#00AAEE
            }
            .title {
                width:1200px;
                margin:0 auto;
                height:80px;
                line-height:80px;
                font-size:20px;
                color:#FFF
            }
            .content {
                width:100%;
                min-width:1200px;
                height:660px;
                background-color:#fff;
            }
            .alipayform {
                width:800px;
                margin:0 auto;
                height:500px;
                border:1px solid #0ae
            }
            .element {
                width:600px;
                height:80px;
                margin-left:100px;
                font-size:20px
            }
            .etitle,.einput {
                float:left;
                height:26px
            }
            .etitle {
                width:150px;
                line-height:26px;
                text-align:right
            }
            .einput {
                width:200px;
                margin-left:20px
            }
            .einput input {
                width:398px;
                height:24px;
                border:1px solid #0ae;
                font-size:16px
            }
            .mark {
                margin-top: 10px;
                width:500px;
                height:30px;
                margin-left:80px;
                line-height:30px;
                font-size:12px;
                color:#999
            }
            .legend {
                margin-left:100px;
                font-size:24px
            }
            .alisubmit {
                width:400px;
                height:40px;
                border:0;
                background-color:#0ae;
                font-size:16px;
                color:#FFF;
                cursor:pointer;
                margin-left:170px
            }
            .footer {
                width:100%;
                height:120px;
                background-color:#242735
            }
            .footer-sub a,span {
                color:#808080;
                font-size:12px;
                text-decoration:none
            }
            .footer-sub a:hover {
                color:#00aeee
            }
            .footer-sub span {
                margin:0 3px
            }
            .footer-sub {
                padding-top:40px;
                height:20px;
                width:600px;
                margin:0 auto;
                text-align:center
            }
        </style>
        <body>
            <div class="header">
                <div class="container black">
                    <div class="qrcode">
                        <div class="littlecode">
                            <img width="16px" src="img/little_qrcode.jpg" id="licode">
                            <div class="showqrs" id="showqrs">
                                <div class="shtoparrow"></div>
                                <div class="guanzhuqr">
                                    <img src="img/guanzhu_qrcode.png" width="80">
                                    <div class="shmsg" style="margin-top:5px;">
                                    请扫码关注
                                    </div>
                                    <div class="shmsg" style="margin-bottom:5px;">
                                        接收重要信息
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="container">
                    <div class="nav">
                        <a href="https://www.alipay.com/" class="logo"><img src="img/alipay_logo.png" height="30px"></a>
                        <span class="divier"></span>
                        <a href="http://open.alipay.com/platform/home.htm" class="open" target="_blank">开放平台</a>
                        <ul class="navbar">
                            <li><a href="https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在线文档</a></li>
                            <li><a href="https://cschannel.alipay.com/portal.htm?sourceId=213" target="_blank">技术支持</a></li>
                        </ul>
                    </div>
                </div>
                <div class="container blue">
                    <div class="title">即时到账批量退款有密接口(refund_fastpay_by_platform_pwd)</div>
                </div>
            </div>
            <div class="content">
                <form action="alipayRefund/submit" class="alipayform" method="post" target="_blank">
                    <div class="element" style="margin-top:60px;">
                        <div class="legend">支付宝即时到账批量退款有密接口快速通道 </div>
                    </div>
                    <div class="element">
                        <div class="etitle">退款批次号:</div>
                        <div class="einput"><input type="text" name="WIDbatch_no" id="WIDbatch_no"></div>
                        <br>
                        <div class="mark">注意：退款批次号(batch_no)，必填(时间格式是yyyyMMddHHmmss+数字或者字母)</div>
                    </div>

                      <div class="element">
                        <div class="etitle">退款笔数:</div>
                        <div class="einput"><input type="text" name="WIDbatch_num" ></div>
                        <br>
                        <div class="mark">注意：退款笔数(batch_num)，必填(值为您退款的笔数,取值1~1000间的整数)</div>
                    </div>
                    <div class="element">
                        <div class="etitle">退款详细数据:</div>
                        <div class="einput"><input type="text" name="WIDdetail_data"></div>
                        <br>
                        <div class="mark">注意：退款详细数据(WIDdetail_data)，必填(支付宝交易号^退款金额^备注)多笔请用#隔开</div>
                    </div>
                    <div class="element">
                        <input type="submit" class="alisubmit" value ="确认支付">
                    </div>
                </form>
            </div>
            <div class="footer">
                <p class="footer-sub">
                    <a href="http://ab.alipay.com/i/index.htm" target="_blank">关于支付宝</a><span>|</span>
                    <a href="https://e.alipay.com/index.htm" target="_blank">商家中心</a><span>|</span>
                    <a href="https://job.alibaba.com/zhaopin/index.htm" target="_blank">诚征英才</a><span>|</span>
                    <a href="http://ab.alipay.com/i/lianxi.htm" target="_blank">联系我们</a><span>|</span>
                    <a href="#" id="international" target="_blank">International&nbsp;Business</a><span>|</span>
                    <a href="http://ab.alipay.com/i/jieshao.htm#en" target="_blank">About Alipay</a>
                    <br>
                     <span>支付宝版权所有</span>
                    <span class="footer-date">2004-2016</span>
                    <span><a href="http://fun.alipay.com/certificate/jyxkz.htm" target="_blank">ICP证：沪B2-20150087</a></span>
                </p>


            </div>
        </body>
        <script>
            var even = document.getElementById("licode");
            var showqrs = document.getElementById("showqrs");
             even.onmouseover = function(){
                showqrs.style.display = "block";
             }
             even.onmouseleave = function(){
                showqrs.style.display = "none";
             }

             var out_trade_no = document.getElementById("out_trade_no");

        </script>

        </html>';


    }


}
